//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

package uxgenes.view;

// #ifdef FOREGROUND_VIEW

import uxgenes.core.Logger;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


//
// UXGEN View Service
//
public class UXViewService 	extends Service
							implements uxgenes.core.EngineContext.ActivityInterface
{
// constants //
	private static final String	TAG = "UXViewService";
	
	
	
// variables //
	protected uxgenes.core.EngineContext		engine;
	
	private boolean		_engineInitialized 	= false;
	private boolean		_engineStarted		= false;
	
	
	
// methods //
	
	// onBind
	@Override
	public IBinder onBind (Intent arg0)
	{
		return null;
	}

	
	// onCreate
	@Override
	public void onCreate ()
	{
	    super.onCreate();
	    engine = new uxgenes.core.EngineContext( this, this );
	}
	

	// onDestroy
	@Override
	public void onDestroy ()
	{
		StopEngine();
		
		if ( !_engineInitialized ) {
			engine.OnDestroy();
			_engineInitialized = false;
		}
		engine = null;
	}
	
	
	// StartEngine
	protected final void StartEngine (ViewGroup rootView, int viewId)
	{
		if ( !_engineInitialized )
		{
			engine.InitEngine( false );
			engine.CreateEngine( rootView, viewId );
			_engineInitialized = true;
		}
		
		if ( !_engineStarted )
		{
			engine.OnStart();
	    	engine.OnResume();
	    	_engineStarted = true;
		}
	}
	
	
	// StopEngine
	protected final void StopEngine ()
	{
		if ( _engineStarted )
		{
			engine.OnPause();
			engine.OnStop();
			_engineStarted = false;
		}
	}
	
	
	
// View Utils //
	
	// SetForegroundView
	//	set view on top of all windows and status bar
	protected final void SetForegroundView (View view, int flags)
	{
    	WindowManager wm =  (WindowManager) getSystemService( WINDOW_SERVICE );
        
		WindowManager.LayoutParams params = new WindowManager.LayoutParams(
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
				WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
				| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
				| flags,
				PixelFormat.TRANSLUCENT );
		
        params.gravity = 53;
	    
        wm.addView( view, params );
	}
	
	
	// SetKeyguardView
	protected final void SetKeyguardView (View view, int flags)
	{
    	WindowManager wm =  (WindowManager) getSystemService( WINDOW_SERVICE );
        
		WindowManager.LayoutParams params = new WindowManager.LayoutParams(
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
				flags
				//| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
				| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
				| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
				| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
				| WindowManager.LayoutParams.FLAG_FULLSCREEN,
				PixelFormat.TRANSLUCENT );
		
        params.gravity = 53;
	    
        wm.addView( view, params );
	}
	
	
	// BuildFlags
	protected static final int BuildFlags (boolean touchable)
	{
		int	flags = 0;
		
		if ( touchable )	flags |= 0;
		else				flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
		
		return flags;
	}
	
	
	
// Utils //
	
	// StartService
	public static final <T> void StartService (Context ctx, Class<T> classType)
	{
		ctx.startService( new Intent( ctx, classType ) );
	}
	
	
	// StopService
	public static final <T> void StopService (Context ctx, Class<T> classType)
	{
		ctx.stopService( new Intent( ctx, classType ) );
	}


	
// EngineContext.ActivityInterface //
	
	// Close
	public final void Close ()
	{
		this.stopSelf();
	}

	// SetRequestedOrientation
	public final void SetRequestedOrientation (int orient)
	{
		Logger.I( TAG, "can't change foreground view orientation" );
		// do nothing
	}
}

// #endif FOREGROUND_VIEW
